package com.lhy.sock;

import java.io.*;
import java.net.Socket;
/*
* 如下的GreetingClient是一个客户端程序，该程序通过
* socket连接到服务器并发送一个请求，然后等待一个响应
* */
public class GreetingClient {
    public static void main(String[] args) {
        //主机名
        String serverName = args[0];
        //端口号
        int port = Integer.parseInt(args[1]);
        try {
            System.out.println("连接到主机： "+serverName+",端口号："+port);
            Socket client = new Socket(serverName, port);
            System.out.println("远程主机地址： "+client.getRemoteSocketAddress());
            OutputStream outputStream = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outputStream);

            out.writeUTF("Hello from "+ client.getLocalSocketAddress());
            InputStream inputStream = client.getInputStream();
            DataInputStream in = new DataInputStream(inputStream);
            System.out.println("服务器响应： "+ in.readUTF());
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
